package cn.tedu.trading_platform.controller;

import cn.tedu.trading_platform.base.response.JsonResult;
import cn.tedu.trading_platform.base.response.StatusCode;
import cn.tedu.trading_platform.mapper.UserMapper;
import cn.tedu.trading_platform.pojo.dto.userDTO.UpdatePasswordParam;
import cn.tedu.trading_platform.pojo.dto.userDTO.UserLoginParam;
import cn.tedu.trading_platform.pojo.dto.userDTO.UserRgeParam;
import cn.tedu.trading_platform.pojo.dto.userDTO.UserUpdateParam;
import cn.tedu.trading_platform.pojo.entity.User;
import cn.tedu.trading_platform.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;

//用户、商家登陆注册、退出、注销、修改信息
@Slf4j
@RestController
@RequestMapping("/user/")
@Api(tags = "用户管理模块")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @PostMapping("reg")
    @ApiOperation("用户注册")
    @ApiOperationSupport(order =1)
    public JsonResult UserReg(@Validated @RequestBody UserRgeParam userRgeParam)
    {
        log.debug("开始处理用户注册请求");
        int i = userMapper.selectByUsername(userRgeParam.getUsername());
        if(i!=0){
            return new JsonResult(StatusCode.USER_EXIST);
        }
        User user = new User();
        BeanUtils.copyProperties(userRgeParam,user);
        user.setCreatedAt(new Date());
        userMapper.insertUser(user);
        return JsonResult.ok();
    }
    @PostMapping("login")
    @ApiOperation("用户登录")
    @ApiOperationSupport(order = 2)
    public JsonResult UserLogin(@Validated @RequestBody UserLoginParam userLoginParam, HttpSession session){
        log.debug("开始处理用户登录请求");
        UserVO userVO = userMapper.selectByUsernameAndPassword(userLoginParam);
        if(userVO==null){
            return new JsonResult(StatusCode.USER_NOT_EXIST_OR_PASSWORD_ERROR);
        }
        session.setAttribute("user",userVO);
        return JsonResult.ok(userVO);
    }
    @PostMapping("update")
    @ApiOperation("用户修改信息,除了密码")
    @ApiOperationSupport(order=3)
    public JsonResult UserUpdate(@Validated @RequestBody UserUpdateParam userUpdateParam, HttpSession session){
        log.debug("开始处理用户修改信息请求");
        UserVO userVO = (UserVO) session.getAttribute("user");
        if(userVO==null){
            return new JsonResult(StatusCode.USER_NOT_LOGIN);
        }
        userUpdateParam.setId(userVO.getId());
        int i = userMapper.updateUser(userUpdateParam);
        if(i==0){
            return new JsonResult(StatusCode.USER_nOT_UPDATE);
        }
        return JsonResult.ok();
    }
    @PostMapping("checkPassword")
    @ApiOperation("用户修改密码")
    @ApiOperationSupport(order =4)
    public JsonResult UserCheckPassword(@Validated @RequestBody UpdatePasswordParam updatePasswordParam, @ApiIgnore HttpSession session){
        log.debug("开始处理用户修改密码请求");
        UserVO userVO = (UserVO) session.getAttribute("user");
        if(userVO==null){
            return new JsonResult(StatusCode.USER_NOT_LOGIN);
        }
        updatePasswordParam.setId(userVO.getId());
        int i = userMapper.updateUserPassword(updatePasswordParam);
        if(i>0){
            session.invalidate();//清除session
            return JsonResult.ok();
        }else{
            return new JsonResult(StatusCode.USER_nOT_UPDATE);
        }
    }
    @PostMapping("del")
    @ApiOperation("用户注销")
    @ApiOperationSupport(order =5)
    public JsonResult deleteUser(HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        Long id = userVO.getId();
        int i = userMapper.deleteUser(id);
        if(i>0){
            session.invalidate(); // 清除 Session
            return JsonResult.ok(StatusCode.OPERATION_SUCCESS);
        }else{
            return JsonResult.fail(StatusCode.USER_NOT_DELETE);
        }
    }

}
